//
//  CDXRootViewController.m
//  Codex
//
//  Created by ChenJie on 2/15/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "CDXRootViewController.h"
#import "OASDK.h"
#import "AppDelegate.h"

@interface CDXRootViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CDXRootViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.tableView];
    
//    self.tableView.backgroundColor = OARandomColor();
    
//    self.tableView.backgroundColor = OAColor(f34f23);
//    self.tableView.backgroundColor = OARGBColor(79, 99, 169, 0.4);
//    self.tableView.backgroundColor = OARandomColor();
    
    
//    NSString *title = @"测试";
//    NSLog(@"title: %@", title);
//    WKLog(@"title: %@", title);
//    
//    NSArray *array = @[title];
//    NSLog(@"array: %@", array);
//    WKLog(@"array: %@", array);
    
    
//    WKLog(@"test: %@", @"t");
    
//    NSString *originalString = @"1.abcde 223 2323 2323 sdfdsf 测试 2323 开发   ";
//    NSLog(@"original:-%@-", originalString);
//    NSLog(@"original:%ld", (long)originalString.length);
//    NSLog(@"trimmed:-%@-", [self trimNonDigit:originalString]);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    self.tableView.oa_top = topMark;
    self.tableView.oa_width = OA_SCREEN_WIDTH;
    self.tableView.oa_height = OA_SCREEN_HEIGHT - topMark;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    NSArray *cells = @[
        @{@"class": UITableViewCell.class,
          @"title": @"Table Model",
          @"target": @"OADTableModelViewController",
        },
        @{@"title": @"Layout 5",
          @"target": @"OADLayout5ViewController",
        },
        @{@"title": @"Layout 6",
          @"target": @"OADLayout6ViewController",
        },
//        @{@"title": @"Lazy Getter",
//          @"target": @"OADLazyGetterViewController",
//        },
//        @{@"title": @"Size",
//          @"target": @"OADSizeViewController",
//        },
//        @{@"title": @"Table Config",
//          @"target": @"OADTableConfigViewController",
//        },
//        @{@"title": @"Layout 7",
//          @"target": @"OADLayout7ViewController",
//        },
//        @{@"title": @"Preset Layout",
//          @"target": @"OADPresetLayoutViewController",
//        },
//        @{@"title": @"Layout",
//          @"target": @"OADLayoutViewController",
//        },
//        @{@"title": @"WKWebView",
//          @"target": @"OADWKWebViewController",
//        },
//        @{@"title": @"WebView2",
//          @"target": @"OADWeb2ViewController",
//        },
//        @{@"title": @"Share",
//          @"target": @"OADShareItViewController",
//        },
//        @{@"title": @"Alert",
//          @"target": @"OADAlertViewController",
//        },
        @{@"title": @"Method Swizzling",
          @"target": @"OADMethodSwizzlingViewController",
        },
        @{@"title": @"View Lifecycle",
          @"target": @"OADViewLifecycleViewController",
        },
        @{@"title": @"Extendable Table",
          @"target": @"OADExtendableTableViewController",
        },
        
    ];
    
    [self.cells addObjectsFromArray:cells];
}

- (Class)cellClassAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class cellClass = cellInfo[@"class"];
    return cellClass ?: UITableViewCell.class;
}

- (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    return cellInfo[@"title"];
}

- (UIViewController *)targetViewControllerAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
    if ([targetVcClass isKindOfClass:NSString.class]) {
        targetVcClass = NSClassFromString((NSString *)targetVcClass);
    }
    
    UIViewController *targetVc = [[targetVcClass alloc] init];
    if ([targetVc isMemberOfClass:UIViewController.class]) {
        targetVc.view.backgroundColor = [UIColor whiteColor];
        targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
    }
    
    return targetVc;
}

- (NSString *)trimNonDigit:(NSString *)originalString {
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    NSString *trimmedString = [[originalString componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
    return trimmedString;
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    OALogFunction;
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    OALogFunction;
    return self.cells.count;
}

#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    OALogFunction;
//    NSLog(@"height for row: (%ld, %ld)", (long)indexPath.section, (long)indexPath.row);
    
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    OALogFunction;
//    NSLog(@"cell for row: (%ld, %ld)", (long)indexPath.section, (long)indexPath.row);
    
    Class cellClass = [self cellClassAtIndexPath:indexPath];
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [self titleAtIndexPath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = [self targetViewControllerAtIndexPath:indexPath];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter



#pragma mark - Getter

- (NSMutableArray *)cells {
    if (!_cells) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        _cells = array;
    }
    return _cells;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableFooterView = [UIView new];
//        tableView.estimatedRowHeight = 0;
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
